package com.gome.ss.core.schedule;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobBuilder;

import com.gome.ss.common.AppConfiguer;
import com.gome.ss.common.Utils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class ScheduJobConfig {
	
	protected static final String SCHEDUJOB_STATUS_INIT = "init";
	protected static final String SCHEDUJOB_STATUS_START = "start";
	protected static final String SCHEDUJOB_STATUS_STOP = "stop";

	private String jobName = "";
	private String jobGroup = "";
	protected String taskID = "";
	
	public ScheduJobConfig () {
		
	}

	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	public String getJobGroup() {
		return jobGroup;
	}

	public void setJobGroup(String jobGroup) {
		this.jobGroup = jobGroup;
	}
	
	public String getTaskID() {
		return taskID;
	}
	
	
	public void setTaskID(String taskID) {
		this.taskID = taskID;
	}
	
	public JobBuilder getJobBuilder(Class<? extends Job> jobClass) {
		JobBuilder jobBuilder = JobBuilder.newJob(jobClass);
		jobBuilder = jobBuilder.withIdentity(this.jobName, this.jobGroup);
		return jobBuilder;
	}
	
	public String createTaskID(String pluginName, String qualifiedName, String pluginQualified) {
		String taskID = pluginName + "." + pluginQualified + "." + Utils.getUUID();
		DBCollection collection = AppConfiguer.ssMongo.getDB(AppConfiguer.SS_DB_NAME)
				.getCollection(AppConfiguer.SS_TASK_COLLECTION_NAME);
		
		BasicDBObject obj = new BasicDBObject();
		obj.put("taskID", taskID);
		obj.put("pluginName", pluginName);
		obj.put("qualifiedName", qualifiedName);
		obj.put("reference", 0);
		obj.put("runningReference", 0);
		obj.put("status", SCHEDUJOB_STATUS_INIT);
		obj.put("isProcessing", false);
		
		collection.insert(obj);
		
		return taskID;
	}
	
	public void updateTaskReference(String pluginName, String qualifiedName, String taskID,
								int reference, int runningReference) {
		DBCollection collection = AppConfiguer.ssMongo.getDB(AppConfiguer.SS_DB_NAME)
				.getCollection(AppConfiguer.SS_TASK_COLLECTION_NAME);
		
		BasicDBObject query = new BasicDBObject();
		query.put("taskID", taskID);
		query.put("pluginName", pluginName);
		query.put("qualifiedName", qualifiedName);
		
		DBCursor cursor = collection.find(query);
		
		if (cursor.count() <= 0)
			return;
		
		DBObject obj = cursor.next();
		obj.put("reference", reference);
		obj.put("runningReference", runningReference);
		collection.update(query, obj);
	}
	
	public void updateTaskStartStatus(String pluginName, String qualifiedName, String taskID) {
		DBCollection collection = AppConfiguer.ssMongo.getDB(AppConfiguer.SS_DB_NAME)
		.getCollection(AppConfiguer.SS_TASK_COLLECTION_NAME);
		
		BasicDBObject query = new BasicDBObject();
		query.put("taskID", taskID);
		query.put("pluginName", pluginName);
		query.put("qualifiedName", qualifiedName);
		
		DBCursor cursor = collection.find(query);
		
		if (cursor.count() <= 0)
			return;
		
		DBObject obj = cursor.next();
		obj.put("status", SCHEDUJOB_STATUS_START);
		obj.put("startDate", (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
		obj.put("isProcessing", true);
		
		collection.update(query, obj);
	}
	
	public void updateTaskStopStatus(String pluginName, String qualifiedName, String taskID, String stopStatus, int totalRef) {
		DBCollection collection = AppConfiguer.ssMongo.getDB(AppConfiguer.SS_DB_NAME)
		.getCollection(AppConfiguer.SS_TASK_COLLECTION_NAME);
		
		BasicDBObject query = new BasicDBObject();
		query.put("taskID", taskID);
		query.put("pluginName", pluginName);
		query.put("qualifiedName", qualifiedName);
		
		DBCursor cursor = collection.find(query);
		
		if (cursor.count() <= 0)
			return;
		
		DBObject obj = cursor.next();
		obj.put("status", SCHEDUJOB_STATUS_STOP);

		Date date = new Date();
		obj.put("stopDate", (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
		obj.put("isProcessing", false);
		obj.put("stopStatus", stopStatus);
		obj.put("totalRef", totalRef);
		
		collection.update(query, obj);
	}
	
	public List<DBObject> getHistoryTaskInfo(String pluginName, String qualifiedName) {
		DBCollection collection = AppConfiguer.ssMongo.getDB(AppConfiguer.SS_DB_NAME)
		.getCollection(AppConfiguer.SS_TASK_COLLECTION_NAME);
		
		BasicDBObject query = new BasicDBObject();
		query.put("status", SCHEDUJOB_STATUS_STOP);
		query.put("pluginName", pluginName);
		query.put("qualifiedName", qualifiedName);
		
		int total = collection.find(query).count();
		DBCursor cursor;
		if (total > 10)
			cursor = collection.find(query).skip(total-10).sort(new BasicDBObject()).limit(10);
		else
			cursor = collection.find(query);
		
		List<DBObject> results = new ArrayList<DBObject>();
		while(cursor.hasNext())
			results.add(cursor.next());
		
		return results;
	}
}
